#include "adduserwidget.h"


AddUserWidget::AddUserWidget(QWidget *parent) : QWidget(parent)
{
    titleLabel = new QLabel("增删账户");
    titleLabel->setFixedHeight(20);
    titleLabel->setFrameStyle(QLabel::StyledPanel);
    titleLabel->setAlignment(Qt::AlignCenter);

    verifyWidget = new QWidget(this);
    verifyLabel = new QLabel("请先验证超级管理员身份");
    verifyLabel->setFixedHeight(40);
    verifyLabel->setFrameStyle(QLabel::StyledPanel);
    verifyLabel->setAlignment(Qt::AlignCenter);
    usernameLabel = new QLabel("用户名：");
    usernameLineEdit = new QLineEdit;
    usernameLineEdit->setEnabled(false);
    usernameLineEdit->setText("Administrator");
    passwordLabel = new QLabel("密码：");
    passwordLineEdit = new QLineEdit;
    passwordLineEdit->setEchoMode(QLineEdit::Password);
    verifyOkButton = new QPushButton("验证");
    verifyOkButton->setEnabled(false);
    QGridLayout* verifyLayout = new QGridLayout(verifyWidget);
    verifyLayout->addWidget(verifyLabel, 0, 0, 1, 5);
    verifyLayout->addWidget(usernameLabel, 1, 1, 1, 1);
    verifyLayout->addWidget(usernameLineEdit, 1, 2, 1, 2);
    verifyLayout->addWidget(passwordLabel, 2, 1, 1, 1);
    verifyLayout->addWidget(passwordLineEdit, 2, 2, 1, 2);
    verifyLayout->addWidget(verifyOkButton, 4, 2, 1, 1);

    changeWidget = new QWidget;
    usernameListWidget = new QListWidget;
    passwordListWidget = new QListWidget;
    nameLabel = new QLabel("操作用户名：");
    nameLineEdit = new QLineEdit;
    passLabel = new QLabel("密码");
    passLineEdit = new QLineEdit;
    addButton = new QPushButton("增加账户");
    addButton->setEnabled(false);
    deleteButton = new QPushButton("删除账户");
    deleteButton->setEnabled(false);
    quitButton = new QPushButton("返回");
    QHBoxLayout* listWidgetLayout = new QHBoxLayout;
    listWidgetLayout->addWidget(usernameListWidget);
    listWidgetLayout->addWidget(passwordListWidget);
    QGridLayout* changeLayout = new QGridLayout(changeWidget);
    changeLayout->addLayout(listWidgetLayout, 0, 1, 4, 5);
    changeLayout->addWidget(nameLabel, 4, 1, 1, 1);
    changeLayout->addWidget(nameLineEdit, 4, 2, 1, 2);
    changeLayout->addWidget(passLabel, 5, 1, 1, 1);
    changeLayout->addWidget(passLineEdit, 5, 2, 1, 2);
    changeLayout->addWidget(addButton, 4, 4, 1, 1);
    changeLayout->addWidget(deleteButton, 5, 4, 1, 1);
    changeLayout->addWidget(quitButton, 5, 5, 1, 1);
    changeWidget->hide();

    QVBoxLayout* topLayout = new QVBoxLayout(this);
    topLayout->addSpacing(20);
    topLayout->addWidget(titleLabel);
    topLayout->addWidget(verifyWidget);
    topLayout->addWidget(changeWidget);

    connect(passwordLineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotVerifyLineEditTextChange()));
    connect(verifyOkButton, SIGNAL(clicked(bool)), this, SLOT(slotVerifyOkButton()));
    connect(nameLineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotLineEditTextChange()));
    connect(passLineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotLineEditTextChange()));
    connect(addButton, SIGNAL(clicked(bool)), this, SLOT(slotAddButton()));
    connect(deleteButton, SIGNAL(clicked(bool)), this, SLOT(slotDeleteButton()));
    connect(quitButton, SIGNAL(clicked(bool)), this, SLOT(slotQuitButton()));
}

void AddUserWidget::slotVerifyOkButton()
{
    QSqlQuery query;
    query.exec(QString("select password from passwordtable where username='Administrator'"));
    query.next();
    if(query.value(0).toString() == passwordLineEdit->text())
    {
        QMessageBox::information(this, "提示", "验证成功！", QMessageBox::Ok);
        showListWidegt();
        verifyWidget->hide();
        changeWidget->show();
    }
    else
    {
        QMessageBox::warning(this, "警告", "帐号与密码不符，验证失败！", QMessageBox::Ok);
    }
}
void AddUserWidget::slotVerifyLineEditTextChange()
{
    if(!passwordLineEdit->text().isEmpty())
    {
        verifyOkButton->setEnabled(true);
    }
    else
    {
        verifyOkButton->setEnabled(false);
    }
}
void AddUserWidget::showListWidegt()
{
    usernameListWidget->clear();
    passwordListWidget->clear();
    usernameListWidget->addItem("用户名");
    passwordListWidget->addItem("密码");
    QSqlQuery query;
    query.exec("select username,password from passwordtable");
    while(query.next())
    {
        usernameListWidget->addItem(query.value(0).toString());
        passwordListWidget->addItem(query.value(1).toString());
    }
}
void AddUserWidget::slotLineEditTextChange()
{
    if(!nameLineEdit->text().isEmpty())
    {
        deleteButton->setEnabled(true);
        if(!passLineEdit->text().isEmpty())
        {
            addButton->setEnabled(true);
        }
        else
        {
            addButton->setEnabled(false);
        }
    }
    else
    {
        addButton->setEnabled(false);
        deleteButton->setEnabled(false);
    }
}
void AddUserWidget::slotAddButton()
{
    QSqlQuery query;
    bool result = query.exec(QString("insert into passwordtable values('%1', '%2')").arg(nameLineEdit->text()).arg(passLineEdit->text()));
    if(result)
    {
        QMessageBox::information(this, "提示", "增添成功！", QMessageBox::Ok);
    }
    else
    {
        QMessageBox::warning(this, "提示", "增添失败！请确认用户已经存在！", QMessageBox::Ok);
    }
    showListWidegt();
}
void AddUserWidget::slotDeleteButton()
{
    QSqlQuery query;
    bool result = query.exec(QString("delete from passwordtable where username='%1'").arg(nameLineEdit->text()));
    if(result)
    {
        QMessageBox::information(this, "提示", "删除成功！", QMessageBox::Ok);
    }
    else
    {
        QMessageBox::warning(this, "提示", "删除失败！请确认是否存在拼写错误！", QMessageBox::Ok);
    }
    showListWidegt();
}
void AddUserWidget::slotQuitButton()
{
    usernameListWidget->clear();
    passwordListWidget->clear();
    nameLineEdit->clear();
    passLineEdit->clear();
    passwordLineEdit->clear();
    changeWidget->hide();
    verifyWidget->show();
}
